# -*- coding: utf-8 -*-
'''
Created on Apr 3, 2012

@author: LONG HOANG GIANG
'''

import sys, os
sys.path.append(os.path.join(os.path.basename(__file__), '../'))
from CrawlerLib.http import Http
import commonlib
import gzip
import simplejson as json
import datetime

cookie = '''=location.href; expires=Mon, 02 Apr 2012 15:16:57 GMT; path=/191/; domain=manga24h.com
=location.href; expires=Mon, 02 Apr 2012 15:17:29 GMT; path=/12854/Jindo-Duong-dan-den-khung-thanh-Tap--98---phan-3/; domain=manga24h.com
=location.href; expires=Tue, 03 Apr 2012 08:53:45 GMT; path=/10099/Jindo-Duong-dan-den-khung-thanh-Tap-1/; domain=manga24h.com
PHPSESSID=r80icqaad157gjlu873ht421v0; path=/; domain=manga24h.com
popads=1; expires=Tue, 03 Apr 2012 07:54:45 GMT; path=/; domain=manga24h.com
=location.href; expires=Tue, 03 Apr 2012 08:56:55 GMT; path=/12/; domain=manga24h.com
tapchi=0; path=/12/; domain=manga24h.com
Ads=Ads%20PopUnder; expires=Tue, 03 Apr 2012 19:57:12 GMT; path=/; domain=manga24h.com
=location.href; expires=Tue, 03 Apr 2012 08:57:12 GMT; path=/1884/Than-Dong-Dat-Viet-Tap-98/; domain=manga24h.com'''


def getChapter(url):
    print 'getChapter url = {0}'.format(url)
    tree = Http.getXMLTree(url, cookie=cookie)
    name = commonlib.getElementText(tree.xpath("//span[@class='manga_name']"))
    images = []
    for item in tree.xpath("//ul[@id='portfolio']/li/img"):
        src = commonlib.getAttribText(item, 'src')
        if src != '':
            src = commonlib.urlJoin('url', src)
            images.append(src)
    return {'chapter': name, 'images': images}


def getListLink():
    url = 'http://manga24h.com/48479/Jarinko-Chie-Chie-co-be-hat-tieu-Chapter-1-To-la-Chie/'
    tree = Http.getXMLTree(url, cookie=cookie)
    data = []
    for item in tree.xpath("//select[@name='chapters']/option"):
        link = item.get('value', '')
        if link != '':
            link = commonlib.urlJoin('http://manga24h.com', link)
            data.append(getChapter(link))
    fp = gzip.open("cobehattieu.gz", 'wb')
    jsondata = json.dumps(data)
    fp.write(jsondata)
    fp.close()
    
        
if __name__ == '__main__':
        
    #getChapter('http://manga24h.com/1884/Than-Dong-Dat-Viet-Tap-98/')        
    getListLink()
    print '>> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)